package mirus;

import org.apache.commons.io.FileUtils;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.apache.commons.io.FileUtils.copyURLToFile;

/**
 * Created by user on 24.10.14.
 */
public class Guns {
    private static final String forumURL = "http://forum.guns.ru/forumtopics/97.html";
    private static final String forumURLPrefix = "http://forum.guns.ru/forummessage/97/";

    private static String getForumURLPage() {
        URL url = null;
        String fileName = "temp.file";
        File file = new File(fileName);
        try {
            url = new URL(forumURL);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            copyURLToFile(url, file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName;
    }

    /**
     * @param htmlRow any String that can contain the html link to the html topic
     * @return index of the html page or 0 if no html link found
     */
    private static int getPageID(String htmlRow) {
        if (!htmlRow.contains("<A HREF=\"" + forumURLPrefix)) {
            return 0;
        }

        Pattern pageIDPattern = Pattern.compile("(\\d+)\\.(html)");
        Matcher matcher = pageIDPattern.matcher(htmlRow);
        if (matcher.find()) {
            return Integer.parseInt(matcher.group(1));
        }
        return 0;
    }

    /**
     * @return latest list of page IDs
     */
    private static ArrayList<Integer> getCurrentLinkList() {
        File file = new File(getForumURLPage());
        String line;
        ArrayList<Integer> currentUrlList = new ArrayList<>();

        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            int pageID;
            while ((line = br.readLine()) != null) {
                pageID = getPageID(line);
                if (pageID != 0) {
                    currentUrlList.add(pageID);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return currentUrlList;
    }

    /**
     * mark all latest available pages as read
     * i.e. put all the latest pages to the Viewed list
     */
    public static void flush() {
        File viewedList = new File("viewedList");
        try {
            FileUtils.writeLines(viewedList, getCurrentLinkList());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static ArrayList<Integer> getViewedLinkList() {
        File viewedList = new File("viewedList");
        if (!viewedList.exists()) {
            System.out.println("*** Viewed list not found, use java -jar filename.jar flush");
            return null;
        }

        ArrayList<Integer> viewedPages = new ArrayList<>();
        Scanner scanner = null;

        try {
            scanner = new Scanner(viewedList);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        while (scanner.hasNext()) {
            if (scanner.hasNextInt())
                viewedPages.add(scanner.nextInt());
        }
        scanner.close();
        return viewedPages;
    }

    /**
     * write the differences between newly downloaded list and already stored one
     */
    public static void getNewPages() {
        List<Integer> currentPages = getCurrentLinkList();
        List<Integer> viewedPages = getViewedLinkList();
        currentPages.removeAll(viewedPages);

        for (Integer pageID : currentPages) {
            System.out.println("\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\" " + forumURLPrefix + pageID + ".html");
        }
    }
}
